<template>
  <div>
    <el-form :inline="true"
             class="coverage-style-searchForm">
      <el-form-item>
        <div class="demo-input-suffix">
          <el-input placeholder="请输入菜单名称"
                    v-model="coreWord"
                    clearable
                    prefix-icon="el-icon-search"
                    class="key-word-serch">
          </el-input>
        </div>
      </el-form-item>
      <el-form-item>
        <el-select v-model="core"
                   clearable>
          <el-option v-for="item in cores"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <button @click="getSelectAll"
                type="button"
                class="el-button el-button--primary el-button--medium h30Btn ">查询</button>
      </el-form-item>
      <el-form-item class="fr">
        <button @click="ceateCustom"
                type="button"
                class="el-button el-button--primary el-button--medium h30Btn ">新建过滤器菜单
        </button>
      </el-form-item>
    </el-form>
    <el-table stripe
              border
              v-loading="loading"
              :header-cell-style="{background:'#F2F3F8'}"
              ref="multipleTable"
              :data="tableData"
              tooltip-effect="dark"
              style="width:100%">
      <el-table-column prop="name"
                       label="菜单名称"
                       align="center"
                       min-width="12%">
        <template slot-scope="scope">
          <span style="color: #2393f1">{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态"
                       align="center"
                       min-width="10%">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 1">启用</span>
          <span v-else>停用</span>
        </template>
      </el-table-column>
      <el-table-column label="属性"
                       align="center"
                       min-width="10%">
        <template slot-scope="scope">
          <span v-if="scope.row.tagId === 1">系统内置</span>
          <span v-else>自定义</span>
        </template>
      </el-table-column>
      <el-table-column label="工单流程"
                       align="center"
                       min-width="10%">
        <template slot-scope="scope"><span class="label-name">{{ scope.row.workOrderTypeName }}</span></template>
      </el-table-column>
      <el-table-column label="可见范围"
                       align="center"
                       min-width="10%">
        <template slot-scope="scope">
          <el-popover trigger="hover"
                      placement="top">
            <p>{{ scope.row.filterMenuVisibleNames }}</p>
            <div slot="reference"
                 class="name-wrapper">
              <p>{{ stringSlice(scope.row.filterMenuVisibleNames) }}</p>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="最近修改时间"
                       align="center"
                       min-width="12%">
        <template slot-scope="scope">{{ timestampToTime(scope.row.lastUpdateTime) }}</template>
      </el-table-column>
      <el-table-column label="最近修改人"
                       align="center"
                       min-width="10%">
        <template slot-scope="scope">{{ scope.row.lastUserName }}</template>
      </el-table-column>
      <el-table-column label="操作"
                       align="center"
                       min-width="25%">
        <template slot-scope="scope">
          <el-button v-if="scope.row.tagId !== 1"
                     type="text"
                     size="mini"
                     class="run-and-stop"
                     @click="handleOff(scope.$index, scope.row)">
            <img v-if="scope.row.status !== 1"
                 src="../../../assets/images/start.png"
                 alt="">
            <img v-else
                 src="../../../assets/images/stop.png"
                 alt="">
            <span v-if="scope.row.status !== 1">启用</span>
            <span v-else>停用</span>
          </el-button>
          <el-button v-if="scope.row.tagId !== 1"
                     icon="el-icon-delete"
                     type="text"
                     size="mini"
                     @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          <el-button icon="el-icon-edit"
                     type="text"
                     size="mini"
                     @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row class="paging-foot"
            :gutter="20">
      <div>
        <el-pagination background
                       class="page-foot-spacing el-pagination-style"
                       v-show="totalDataLength > 0"
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="page"
                       :page-sizes="[10, 20, 50, 100]"
                       :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="totalDataLength"></el-pagination>
      </div>
    </el-row>
  </div>
</template>
<script>
import { deleteFilterMenu, selectByNameAndOrderTypeName, selectAllSelectable, updateUseStatus } from '@/api/filterMenu'
import common from '@/common/common.js'
export default {
  data () {
    return {
      loading: true,
      totalDataLength: 1,
      pageSize: 10,
      page: 1,
      coo: 1,
      tableData: [],
      cores: [],
      core: 'all',
      coreWord: ''
    }
  },
  mounted () {
    this.getSelectAll()
    this.getType()
  },
  methods: {
    stringSlice (str) {
      if (!str) {
        return
      }
      if (str.length > 3) {
        str = str.substring(0, 3) + '...'
        return str
      } else {
        return str
      }
    },
    getType () {
      selectAllSelectable().then(res => {
        let data = res.data
        if (data.retCode === 200) {
          this.cores = data.data
        }
      })
    },
    handleEdit (index, row) {
      this.$store.commit('filterMenuIdFun', row.id)
      this.$router.push('/customEditMenu')
    },
    handleOff (index, row) {
      let status = row.status
      if (status === 1) {
        status = 0
      } else {
        status = 1
      }
      let params = {
        id: row.id,
        userId: this.$store.state.userId,
        status: status
      }
      updateUseStatus(params).then(res => {
        let data = res.data
        if (data.retCode === 200) {
          this.$message({
            type: 'success',
            message: data.msg
          })
          // this.getSelectAll()
          this.getSelectAllData()
        }
      })
    },
    getSelectAll () {
      this.page = 1
      this.getSelectAllData()
    },
    // 封装
    getSelectAllData () {
      let params = {
        name: this.coreWord,
        workOrderTypeId: this.core + '',
        currentPage: this.page,
        pageSize: this.pageSize
      }
      selectByNameAndOrderTypeName(params).then(res => {
        let data = res.data
        if (data.retCode === 200) {
          this.tableData = data.data
          this.totalDataLength = data.count
          this.loading = false
          if (this.tableData.length <= 0) {
            this.page = this.page - 1
            this.getSelectAllData()
          }
        }
      })
    },
    handleSizeChange (pageSize) {
      this.pageSize = pageSize
      this.page = 1
      // this.getSelectAll()
      this.getSelectAllData()
    },
    handleCurrentChange (page) {
      this.page = page
      // this.getSelectAll()
      this.getSelectAllData()
    },
    handleDelete (index, row) {
      this.$confirm('确认要删除吗？', '确认删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        center: true,
        customClass: 'messageBox-prompt-delete'
      }).then(() => {
        let id = row.id
        deleteFilterMenu(id).then(res => {
          let data = res.data
          if (data.retCode === 200) {
            this.$message({
              type: 'success',
              message: data.msg
            })
            // this.getSelectAll()
            this.getSelectAllData()
          }
        })
      })
    },
    ceateCustom () {
      this.$store.commit('filterMenuIdFun', '')
      this.$router.push('/customEditMenu')
    },
    deleteSelect () {
    },
    // 时间戳的转换
    timestampToTime (timestamp) {
      return common.timestampToTime(timestamp)
    }
  }
}
</script>
<style scoped>
.key-word-serch {
  min-width: 300px;
}

.el-pagination {
  text-align: right;
}

>>> .el-input__icon {
  font-size: 13px;
  font-weight: 800;
}

>>> .el-input--prefix .el-input__inner {
  padding-left: 35px;
}
.el-pagination-style {
  margin: 15px 0;
}
</style>
